package com.bianyuanren.v4.utils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

/**
 * @author 微笑的你
 * 七牛云的token
 * @version 1.0
 * @date 2020/5/4 0004 下午 4:20
 */
public class QiniuTokenUtils {

    // 设置需要操作的账号的AK和SK
    private static final String ACCESS_KEY = "Oy63b47SsWt-2-CT5PLLhKl3YuweeuV4PJhpL0KS";
    private static final String SECRET_KEY = "dov6bhSYXXMGP0sxplLVTjwTy7dZYBeRUKbIsPVa";
    // 要上传的空间
    private static final String bucketname ="pinsgle";
    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    private static final String DOMAIN = "http://qiniu.huarenair.com/";


    /**
     * 后台生成七牛云上传的token
     */
    /*public static QiNiu getToken() {
        QiNiu qiNiu = new QiNiu();
        String accessKey = "";
        String secretKey = "";
        String bucket = "";
        long expireSeconds = 600;   //过期时间
        StringMap putPolicy = new StringMap();
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket,null, expireSeconds,putPolicy);
        qiNiu.setKey(UUID.randomUUID().toString().replaceAll("\\-", ""));
        qiNiu.setToken(upToken);
        return qiNiu;
    };*/

    public static String getToken() {
        long expireSeconds = 600;   //过期时间
        StringMap putPolicy = new StringMap();
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(bucketname,null, expireSeconds,putPolicy);
       /* qiNiu.setKey(UUID.randomUUID().toString().replaceAll("\\-", ""));
        qiNiu.setToken(upToken);*/
        return upToken;
    };


    /**
     * 七牛云图片鉴黄
     */
    public static Boolean pornography(String pis){
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String url = "http://ai.qiniuapi.com/v3/image/censor";
        String host = "ai.qiniuapi.com";

        String body = "{ \"data\": { \"uri\": \""+pis+"\" }, \"params\": { \"scenes\": [ \"pulp\", \"terror\", \"politician\" ] } }";
        String contentType = "application/json";

        String method = "POST";
        String qiniuToken = "Qiniu " + auth.signRequestV2(url, method, body.getBytes(), contentType);

        StringMap header = new StringMap();
        header.put("Host", host);
        header.put("Authorization", qiniuToken);
        header.put("Content-Type", contentType);
        Client client = new Client();
        Response response = null;
        String str ="";
        try {
            response = client.post(url, body.getBytes(), header, contentType);
            str = response.bodyString();
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        if (!str.equals("")){
            JsonParser jp = new JsonParser();
            //将json字符串转成json对象
            JsonObject jo = jp.parse(str).getAsJsonObject();
            String label = jo.get("result").getAsJsonObject().get("scenes").getAsJsonObject()
                    .get("pulp").getAsJsonObject().get("details").getAsJsonArray().get(0).getAsJsonObject().get("label").getAsString();
            if (label.equals("pulp")){
                return false;
            }
        }
        return true;
    }

}
